import { getCurrentInstance } from "./component";



export function provide(key,value) {
    //存
    const currentInstance:any = getCurrentInstance();
    
    //只有在setup中是用getCurrentInstance才会有值
    if (currentInstance){
        let { provides } = currentInstance;
        const parentProvides = currentInstance.parent.provides;
        // init
        if (provides === parentProvides){
            provides = currentInstance.provides =  Object.create(parentProvides);

        }
        

        provides[key] = value;
    }
}
export function inject(key,defaultValue) {
    //取
    const currentInstance:any = getCurrentInstance();
    if (currentInstance) {
       
        const parentProvides = currentInstance.parent.provides;
        if (key in parentProvides){
            return parentProvides[key];
        } else {
            if (typeof defaultValue === "function"){
                return defaultValue();
            }
            return defaultValue;
        }
        
    }
}